package com.dstz.bpm.core.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class XmlCovertUtil {
   @SafeVarargs
   public static Object a(String xml, Class<? extends Object>... classes) throws JAXBException, UnsupportedEncodingException {
      JAXBContext jAXBContext = JAXBContext.newInstance(classes);
      Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
      InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
      return unmarshaller.unmarshal(is);
   }

   public static String a(Object serObj) throws JAXBException {
      JAXBContext jc = JAXBContext.newInstance(serObj.getClass());
      StringWriter out = new StringWriter();
      Marshaller m = jc.createMarshaller();
      m.setProperty("jaxb.formatted.output", true);
      m.setProperty("jaxb.encoding", "utf-8");
      m.marshal(serObj, out);
      String tmp = out.toString();
      return tmp;
   }
}
